<!-- 知识库 -->
<template>
  <div class="knowledge animate__animated animate__zoomIn">
    <!-- search-box -->
    <div v-if="!loadingFlag" class="k-top">
      <div class="knowledge-title">
        <i class="el-icon-tickets"></i>
        <h1>
          <span style="color: #fa7f7f">知</span>
          <span style="color: #89cf5e">识</span>
          <span style="color: #fbe27f">库</span>
        </h1>
      </div>
      <div class="knowledge-type">
        <el-radio-group v-model="radio" @change="handleType">
          <el-radio :label="''" class="all-kg">全部</el-radio>
          <!-- <el-radio :label="1">法律法规</el-radio>
          <el-radio :label="2">应急救援案例</el-radio>
          <el-radio :label="3">特种设备事故案例</el-radio>
          <el-radio :label="4">应急救援预案</el-radio>
          <el-radio :label="5">视频资料</el-radio> -->
          <el-radio v-for="(item, i) in types" :key="i" :label="item.id">{{
            item.name
          }}</el-radio>
        </el-radio-group>
      </div>
      <div class="knowledge-input-box">
        <div class="search-btn" @click="kgSearch">
          <i class="el-icon-search"></i>
        </div>
        <div class="kg-search">
          <el-input
           v-model="inputStr" 
           :placeholder="placeholder"
           @focus="isFocus=true"
           @blur="isFocus=false"
           ></el-input>
        </div>
      </div>
      <div class="back"><GoBack></GoBack></div>
    </div>

    <!-- k-list -->
    <div v-if="kgShow" class="k-list animate__animated animate__fadeInLeft">
      <div class="list-info">
        <div class="info-box">
          <template v-if="!kgNone">
            <div v-if="!kgNone" class="info-li kg-head">
            <span>标题</span>
            <span>时间</span>
          </div>
          <div class="info-li" v-for="(item, i) in kgData" :key="i" @click="detailById(item.id)">
            <span>{{ item.title }}</span>
            <span>{{ item.create_time }}</span>
          </div>
          </template>
          
          <div v-if="kgNone" class="kg-none">
            <span>无结果</span>
          </div>
        </div>
      </div>
    </div>

    <div v-if="loadingFlag" class="e-loading">
      <dv-loading style="color: #fff; opcity: 0.5"
        >设备列表数据加载中...</dv-loading
      >
    </div>
  </div>
</template>

<script>

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {  },
  data() {
    //这里存放数据
    return {
      kgType: "全部类别",
      radio: '',
      inputStr: "",
      placeholder: "资料名",
      loadingFlag: true,
      types: [],
      kgShow: false,
      kgData: [],
      kgNone: false,
      isFocus: false
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    handleType(command) {
      console.log(command);
      console.log(this.radio);
    },

    detailById(id) {
      console.log("准备发送的知识库ID: " + id);
      let params = {id}
      this.$router.push({
        name:'kg-detail',
        params
      })
      
    },

    // 处理搜索
    async kgSearch() {
      if (this.inputStr.trim() == '') {
        this.$notify({
          title: '提示',
          message: '搜索文字不能为空！',
          offset: 100,
          duration: 2000,
          type: 'warning'
        });
        return
      }

      this.kgData = []
      // this.kgShow = false;
      this.kgNone = false

      let params = {
        typeId: this.radio,
        title: this.inputStr,
      };

      console.log("搜索的知识库的数据:");
      console.log(params);

      let ret = await this.$http.postGetKnowledgeList(params);
      if (ret.data.errorCode != 0) {
        return console.log("知识库列表后台出错");
      }
      let data = ret.data.data;
      console.log(data);

      if (data.length < 1) {
        console.log("无结果");
        this.$notify({
          title: '提示',
          message: '没有搜索到结果',
          offset: 100,
          duration: 1000,
          type: 'warning'
        });
        this.kgNone = true
        return 
      }

      this.$nextTick(() => {
        this.kgData = data;
        this.kgShow = true;
      });
    },

    // 获取类别
    async getKgTypes() {
      let ret = await this.$http.postGetKnowledgeTypeList();
      this.types = ret.data.data;
      console.log(this.types);
      this.loadingFlag = false;
    },

    // init
    async init() {
      // 获取类别
      this.getKgTypes();
    },

    handleKeyUp() {
      // console.log('注册键盘事件...');
      var that = this;
      document.onkeyup = function (e) {
        if (e.keyCode === 13 && that.isFocus === true) {
          that.kgSearch()
        }
      };
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.init();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.handleKeyUp()
  },
  beforeRouteEnter(to, from, next) {
    console.log("beforeRouteEnter");
    // console.log(to);
    // console.log(from);
    next();
  },
};
</script>
<style lang='less' scoped>
//@import url(); 引入公共css类
.el-radio-group {
  .el-radio {
    .el-radio__label {
      font-size: 0.2rem !important;
    }
  }
}
.knowledge {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: rgba(0, 16, 83, 0.1);
  position: relative;
  .k-top {
    width: 100%;
    // height: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: rgba(0, 16, 83, 0.1);
    position: relative;
    .knowledge-input-box,
    .knowledge-type,
    .knowledge-title {
      width: 44%;
      height: 0.6rem;
      padding: 1% 0;
      position: relative;
      .search-btn {
        position: absolute;
        width: 0.5rem;
        height: 100%;
        right: 0;
        top: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        i {
          display: block;
          font-size: 0.4rem;
          color: @FontColor;
          cursor: pointer;
          z-index: 99999;
        }
      }
    }
    .knowledge-type {
      width: 100%;
    }
    .knowledge-title {
      display: flex;
      justify-content: center;
      align-items: center;
      h1,
      i {
        font-size: 0.6rem;
        color: @FontColor;
      }
      i {
        color: #3cedff;
        padding-right: 2%;
      }
    }
    .knowledge-input-box {
      display: flex;
      justify-content: space-between;
      .kg-search {
        width: 100%;
        height: 100%;
      }
    }
    .knowledge-type {
      height: 0.2rem;
      line-height: 0.2rem;
      font-size: 0.2rem;
    }
  }
  .k-list {
    width: 100%;
    flex: 3;
    overflow: hidden;
    // display: none;
    .list-info {
      width: 70%;
      height: 95%;
      margin: 1% auto 0;
      border-radius: 1%;
      border: 1px solid #ccc;

      box-sizing: border-box;

      overflow: auto;
      padding-top: 0.6rem;
      .info-box {
        width: 75%;
        height: 98%;
        margin: 0 auto;
        position: relative;
        .info-li {
          width: 100%;
          height: 0.6rem;
          display: flex;
          justify-content: space-between;
          align-items: center;
          span {
            font-size: 0.22rem;
            color: #eee;
          }
        }
        .info-li.kg-head {
          span {
            font-size: 0.24rem;
            font-weight: 700;
            color: @Green;
          }
        }
        .kg-none {
          position: absolute;
          left: 0;
          top: 0;
          right: 0;
          bottom: 0;
          z-index: 99999;
          display: flex;
          justify-content: center;
          align-items: center;
          span {
            font-size: 0.3rem;
            color:#eee;
          }
        }
      }
    }
  }

  .back {
    position: absolute;
    left: 0;
    top: 1%;
  }

  .kg-show {
    display: block;
    flex: 3;
  }
}
</style>